package com.cloud.force.api.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.force.api.bean.HumanDetail;
import com.cloud.force.api.bean.LawCauseItem;
import com.cloud.force.api.bean.LawCauseSearchCriteria;
import com.cloud.force.api.bean.RecForceForm;
import com.cloud.force.api.bean.RecForceSearchCriteria;
import com.cloud.force.api.bean.RecForceStatCriteria;
import com.cloud.force.api.service.RecForceService;
import com.cloud.force.common.consts.ForceConsts;
import com.cloud.force.common.context.HumanSession;
import com.cloud.force.common.exception.BizException;
import com.cloud.force.common.result.Quokka;
import com.cloud.force.common.result.SelectItem;
import com.cloud.force.common.utils.SessionHelper;
import com.cloud.force.interfaces.FeignClientLawCase;
import com.cloud.force.interfaces.FeignClientSystem;
import com.github.pagehelper.PageInfo;


@RestController
@RequestMapping("force/api/rec")
public class RecForceController {

	@Autowired
	private FeignClientSystem feignClientSystem;
	
	@Autowired
	private RecForceService recForceService;
	
	@Autowired
	private FeignClientLawCase feignClientLawCase;
	
	/**
	 * 获取行政强制字典项
	 * @param sysId
	 * @return
	 */
	@GetMapping("dicts")
	public Quokka loadDicts(Long sysId) {
		Quokka qk = new Quokka(true);
		Map<String, Object> map = new HashMap<String, Object>();
		List<SelectItem> items = feignClientSystem.findSubjectDomain(SessionHelper.get().getUnitId());
		if(null == items || items.size() == 0){
			throw new BizException(500, "执法机关所属执法领域为配置！");
		}
		map.put("domains", items);
		map.put("litigantTypes", feignClientSystem.loadDictItems(sysId, ForceConsts.FORCE_LITIGANT_TYPES));// 当事人类型
		map.put("eventSrcTypes", feignClientSystem.loadDictItems(sysId, ForceConsts.FORCE_EVENT_SOURCE_TYPES));// 案件来源
		map.put("idCardTypes", feignClientSystem.loadDictItems(sysId, ForceConsts.FORCE_IDCARD_TYPES));// 证件类型
		map.put("measureTypes", feignClientSystem.loadDictItems(sysId, ForceConsts.FORCE_MEASURE_TYPES));// 强制措施种类
		map.put("receiveTypes", feignClientSystem.loadDictItems(sysId, ForceConsts.FORCE_RECEIVE_TYPES));//送达方式
		map.put("measureHandleTypes", feignClientSystem.loadDictItems(sysId, ForceConsts.FORCE_MEASURE_HANDLE_TYPES));//强制措施处理方式
		map.put("executeTypes", feignClientSystem.loadDictItems(sysId, ForceConsts.FORCE_EXECUTE_TYPES));//
		map.put("forceCodeTypes", feignClientSystem.loadDictItems(sysId, ForceConsts.FORCE_CODE_TYPES));//代码类型
		qk.setData(map);
		return qk;
	}
	
	/**
	 * 保存更新行政强制信息
	 * @param force
	 * @return
	 */
	@PostMapping("saveupdate")
	public Quokka savaUpdate(@RequestBody RecForceForm force){
		return new Quokka(true, recForceService.saveUpdate(force));
	}
	
	/**
	 * 加载行政强制详细信息
	 * @param recId
	 * @return
	 */
	@GetMapping("load")
	public Quokka load(long recId){
		return new Quokka(true, recForceService.load(recId));
	}
	
	/**
	 * 行政强制案由检索
	 * @param criteria
	 * @return
	 */
	@PostMapping("searchlawcause")
	public Quokka searchLawCause(@RequestBody LawCauseSearchCriteria criteria){
		PageInfo<LawCauseItem> pageInfo = feignClientLawCase.lawCausePagination(criteria);
		return new Quokka(true, pageInfo);
	}
	
	/**
	 * 行政强制查询
	 * @param criteria
	 * @return
	 */
	@PostMapping("search")
	public Quokka search(@RequestBody RecForceSearchCriteria criteria){
		HumanSession session = SessionHelper.get();
		if(session.getHumanId() > 0){
			List<SelectItem> unitDomainItems = feignClientSystem.loadUnitDomains(SessionHelper.get().getUnitId());
			for(SelectItem item : unitDomainItems){
				criteria.getDomains().add((String)item.getValue());
			}
			HumanDetail humanDetail = feignClientSystem.getHumanDetail(session.getHumanId());
			criteria.setUnitCode(humanDetail.getUnitCode());
			criteria.setRegionCode(humanDetail.getRegionCode());
		}
		return new Quokka(true, recForceService.searchForcePage(criteria));
	}
	
	/**
	 * 行政强制统计功能
	 * @param criteria
	 * @return
	 */
	@PostMapping("stat")
	public Quokka stat(@RequestBody RecForceStatCriteria criteria){
		HumanSession session = SessionHelper.get();
		if(session.getHumanId() > 0){
			List<SelectItem> unitDomainItems = feignClientSystem.loadUnitDomains(SessionHelper.get().getUnitId());
			for(SelectItem item : unitDomainItems){
				criteria.getDomains().add((String)item.getValue());
			}
			HumanDetail humanDetail = feignClientSystem.getHumanDetail(session.getHumanId());
			criteria.setRegionCode(humanDetail.getRegionCode());
		}
		return new Quokka(true, recForceService.statForcePage(criteria));
	}
	
	/**
	 * 主协办人自动补全
	 * @param query
	 * @return
	 */
	@GetMapping("searchlawyer")
	public Quokka searchLawyer(String query){
		long unitId = SessionHelper.get().getUnitId();
		return new Quokka(true, feignClientSystem.searchLawyerByUnit(unitId, query));
	}
	
	@RequestMapping("cancel")
	public Quokka cancel(@RequestParam(required=true) Long recId){
		recForceService.cancel(recId);
		return new Quokka(true);
	}

}
